
import { Context } from 'koa';
import { validateDto } from '@/utils/validateDto';
import { CreateIndexInfoDto, UpdateIndexInfoDto, QueryIndexInfoDto, DeleteIndexInfoDto, SelectOneIndexInfoDto } from '@/dto/indexInfo.dto';
import indexInfoService from '@/services/indexInfo.service';

class IndexInfoController {
    async selectList(ctx: Context) {

        const reqData = ctx.request.body as QueryIndexInfoDto;
        const errors = await validateDto(QueryIndexInfoDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        let resData = await indexInfoService.selectList(reqData)
        ctx.body = {
            code: 200,
            message: "列表查询成功",
            data:resData
        };
    }
    async selectOne(ctx: Context) {
        const reqData = ctx.request.body as SelectOneIndexInfoDto;
        let resData = await indexInfoService.selectOne(reqData)

        
        ctx.body = {
            code: 200,
            message:"详情查询成功",
            data: resData,

        };
    }

    async add(ctx: Context) {
        const userId = ctx.state.userId as number;
        const reqData = ctx.request.body as CreateIndexInfoDto;
        //reqData.userId = reqData.userId || userId; //暂时注释
        const errors = await validateDto(CreateIndexInfoDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await indexInfoService.add(reqData);   
        
        ctx.body = {
        code: 200,
        message:"添加成功",
        data:{id:resData.id},
        };

    }

    async update(ctx: Context) {
        const reqData = <UpdateIndexInfoDto>ctx.request.body
        const errors = await validateDto(UpdateIndexInfoDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await indexInfoService.update(reqData);   


        ctx.body = {
            code: 200,
            message:"更新成功",
            data: {},
        };

    }
    async delete(ctx: Context) {
        const reqData = ctx.request.body as DeleteIndexInfoDto;
        const errors = await validateDto(DeleteIndexInfoDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await indexInfoService.delete(reqData);

        ctx.body = {
            code: 200,
            message:"删除成功",
            data: {},
        };

    }


}

export default new IndexInfoController();   
    
    